/**
 * 供应商supplier.js
 * 版本号： v1.0.0
 */
$(function () {

    //设置输入框的最大输入长度
    $(".easyui-textbox").each(function(){
        var data_options = $(this).attr("data-options");
        if(data_options){
            var matchs = data_options.match(/length\[1,(\d*)\]/);
            if(matchs){
                $(this).textbox("textbox").attr("maxlength", matchs[1] - 0 + 1);
            }else{
                $(this).textbox("textbox").attr("maxlength", 20);
            }
        }else{
            $(this).textbox("textbox").attr("maxlength", 20);
        }
    });


    /**
     * 供应商列表
     */
    var lastChecked=0;
     $("#supplier_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/supplier/supplierList.do',
        idField: 'supId',
        frozenColumns: [[
            {field: 'supId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'supName', title: '供应商名称', width: 200, fixed: true},
                {field: 'socialCode', title: '供应商社会信用代码', width: 150, fixed: true},
                {field: 'contacts', title: '联系人', width: 100, fixed: true},
                {field: 'mobile', title: '联系电话', width: 120},
            ]
        ],
         onCheck:function(rowIndex, rowData){
             var row = $("#supplier_dataList").datagrid("getChecked");
             if(row && rowIndex == lastChecked){
                 $('#supplier_dataList').datagrid("unselectAll");//取消选中当前所有行
                 lastChecked = null;
             }else{
                 lastChecked = rowIndex;
             }
         },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#supplier_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#supplier_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });


});



/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            supplier_loadSearch();
                        }
                    });
                }
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                                id:btnOptions[i].value.meId,
                                text:btnOptions[i].value.name,
                                iconCls:btnOptions[i].value.iconCls,
                                handler: function () {
                                    window.location.href = CONTEXT_PATH + "/supplier/toSupplier.do";
                                }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                                id:btnOptions[i].value.meId,
                                text:btnOptions[i].value.name,
                                iconCls:btnOptions[i].value.iconCls,
                                handler: function () {
                                    supplier_edit();
                                }
                    });
                }
                //删除按钮
                if("delBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            supplier_del();
                        }
                    });
                }
                //查看详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                                id:btnOptions[i].value.meId,
                                text:btnOptions[i].value.name,
                                iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            supplier_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查看供应商
 */
function supplier_view(iconCls){
    var row = $("#supplier_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的供应商详情", timeout: 3000, showType: "slide" });
        return;
    }
    var row = $("#supplier_dataList").datagrid("getSelected");
    //IFRAM打开窗口
    layoutSub(gysxq.id, gysxq.url + row.supId, iconCls, gysxq.closable, gysxq.title);
}


/**
 * 删除供应商信息
 */
function supplier_del() {
    var checked = $("#supplier_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要删除的供应商信息", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#supplier_dataList").datagrid("getSelected");
    $.messager.confirm('删除提示', '您确定要删除 ' + row.supName + ' 吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/supplier/del.do?supId=" + row.supId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#supplier_dataList").datagrid("reload", {
                        supName: $("#supName_").textbox("getValue"),
                        socialCode: $("#socialCode_").textbox("getValue")
                    });
                }
            })
        }
    });
}


/**
 * 修改供应商信息
 * @returns {boolean}
 */
function supplier_edit() {
    var checked = $("#supplier_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择要修改的供应商信息", timeout: 3000, showType: "slide" });
        return;
    };
    var row = $("#supplier_dataList").datagrid("getSelected");
    window.location.href = CONTEXT_PATH + "/supplier/toSupplier.do?supId=" + row.supId;
}


/**
 *  加载查询
 */
function supplier_loadSearch() {
    //刷新dg页码
    refreshPageNumber("supplier_dataList");
    //查询数据
    $("#supplier_dataList").datagrid("reload", {
        supName: $("#supName_").textbox("getValue"),
        socialCode: $("#socialCode_").textbox("getValue")
    });
}











